//
//  SettingController.m
//  quickLook
//
//  Created by J on 15/8/30.
//  Copyright (c) 2015年 JZJ. All rights reserved.
//

#import "SettingController.h"

#import "feedbackController.h"

#import "aboutUs.h"
#import "Header.h"

@interface SettingController ()<UITableViewDelegate,UITableViewDataSource,UMSocialUIDelegate,UIAlertViewDelegate>


@property(nonatomic,strong)UITableView* tableView;

@property(nonatomic,strong)NSArray* titles;


@property(nonatomic,strong)NSArray * iconArray;

@end

@implementation SettingController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createUI];
}


- (void)createUI
{
    
    
    //去掉返回按钮文字 保留箭头
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
                                                         forBarMetrics:UIBarMetricsDefault];
    
   
    self.navigationController.navigationBar.barStyle = UIStatusBarStyleDefault;
    

    
    self.title=@"设置";
    self.view.backgroundColor=[UIColor whiteColor];
    _tableView=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    
    [self.view addSubview:_tableView];
    
    _titles=@[@[@"技术支持",@"意见反馈",@"推荐给朋友"],@[@"清理缓存",@"关于"]];
    _iconArray=@[@[@"round30.png",@"speech54.png",@"thumbs4.png"],@[@"trash27.png",@"compact8.png"]];

    
    
    
    NSUserDefaults* userdefault=[NSUserDefaults standardUserDefaults];
    
    NSDictionary* dic= [userdefault objectForKey:DATADIC];

    _dataDic=dic;
    

}

#pragma mark --UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if (section==1) {
        
        
        
        return 2;
    }else if(section==2)
    {
        
        return 1;
     
        
        
    }else
        
    {
    
    
        return 3;

    }
    
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

 
    
    return 2;
 
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:@"ID"];
    
    if (!cell) {
        
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ID"];
        
        cell.selectionStyle=UITableViewCellSeparatorStyleNone;
        
    }
    
    
    switch (indexPath.section) {
        case 0:
        
        
            switch (indexPath.row) {
                case 1:
                {
                
                    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

                }
                    break;
                    
                default:
                    break;
            }
            
        
            break;
            
            
        case 1:
            
            switch (indexPath.row) {
             
                case 0:
                    
                {
                  UILabel*  lable=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 150,44)];
                    cell.accessoryView = lable;

                    lable.tag=10000;

                   lable.textColor=JZJLABELCOLOR;
                    lable.font=[UIFont systemFontOfSize:12];

                    lable.textAlignment=NSTextAlignmentRight;
                    
                    
                    lable.text=[NSString stringWithFormat:@"缓存 %.1f M",[[SDImageCache sharedImageCache] getSize]/1024.0/1024.0];
                    
                        
                }
                    
                    break;
                
                case 1:
                    
                {
                
                
                    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

                }
        
                    break;
                    
        
        
        }
            break;
            
            
       case 2:
            
            
        {
        
            UIButton* button=[UIButton buttonWithType:UIButtonTypeCustom];
            
            CGRect frame=cell.contentView.frame;
            
            button.frame=CGRectMake(20, 0, SCREEN_WIDTH-40, frame.size.height);
            
            
//            button.center=cell.contentView.center;
            button.backgroundColor=[UIColor orangeColor];
            [button setTitle:@"退出账号" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

            button.layer.cornerRadius=15;
            [button addTarget:self action:@selector(quit:) forControlEvents:UIControlEventTouchUpInside];
            
            [cell.contentView addSubview:button];
            cell.contentView.backgroundColor=_tableView.backgroundColor;
    
            return cell;
        
        
        }
            
            break;
       
    }
    
    
   
    
    

   
    //刷新cell
    
    cell.textLabel.text=_titles[indexPath.section][indexPath.row];
    
    cell.textLabel.font=[UIFont systemFontOfSize:15];
    cell.textLabel.textColor=JZJLABELCOLOR;
    
    
    
    
    cell.imageView.image=[UIImage imageNamed:_iconArray[indexPath.section][indexPath.row]];
    
    
    
    
    CGSize itemSize = CGSizeMake(25, 25);
    UIGraphicsBeginImageContextWithOptions(itemSize, NO, UIScreen.mainScreen.scale);
    CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
    [cell.imageView.image drawInRect:imageRect];
    cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    
    

    
    return cell;

}


- (void)quit:(UIButton*)sender
{

    UIAlertView* alert=[[UIAlertView alloc]initWithTitle:@"是否退出账号" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    
    

    
    alert.alertViewStyle=UIAlertControllerStyleActionSheet;
    
    
    [alert show];
    


}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    
    switch (indexPath.section) {
        case 0:
            
            switch (indexPath.row) {
                    
                    
                case 0:
                    
                    
                {
                
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://blog.sina.com.cn/u/3106243403"]];

                
                }
                    
                    break;
                    
                    
                    
                case 1:
                    
                {
                    feedbackController* vc=[[feedbackController alloc]init];
                    
                    vc.hidesBottomBarWhenPushed=YES;
                    
                    [self.navigationController pushViewController:vc animated:YES];
                }
                    break;
                    
                case 2:
                {
                
                    
                    [UMSocialSnsService presentSnsIconSheetView:self
                                                         appKey:@"55c834b667e58e36bd0004fe"
                                                      shareText:@"你要分享的文字"
                                                     shareImage:[UIImage imageNamed:@"icon.png"]
                                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToEmail,UMShareToSms,UMShareToRenren,nil]
                                                       delegate:self];
                    

                    
                    
                }
                    
                    break;
  
                    
                
            
            }
            
            break;
            
        case  1:
            
            switch (indexPath.row) {
                case 0:
                    
                {
                
                    
                    [MMProgressHUD setPresentationStyle:MMProgressHUDPresentationStyleFade];
                    [MMProgressHUD showWithStatus:nil];
                    
                    
                    //清除disk缓存
                    [[[SDWebImageManager sharedManager] imageCache] clearDisk];
                    //        清除内存中缓存
                    [[[SDWebImageManager sharedManager] imageCache] clearMemory];
                    
                    
                    [MMProgressHUD dismiss];
                    
                    
                    
                    
                    
                    
                    UILabel* lable=(UILabel*)[self.view viewWithTag:10000];
                    lable.textAlignment=NSTextAlignmentRight;
                    
                    lable.text=@"0 M";
                    

                
                
                }
                    break;
                    
                default:
                    break;
            }
            
            break;
    }
    

    if  (indexPath.section==1&&indexPath.row)
    {
        
        aboutUs *us=[[aboutUs alloc]init];

        
        us.hidesBottomBarWhenPushed=YES;

        [self.navigationController pushViewController:us animated:YES];

        
    }




}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{


    return 2;

}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section
{

    return 20;

}



//点击退出登录
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{

    if (buttonIndex==1) {
        
        
        NSUserDefaults* userdefault=[NSUserDefaults standardUserDefaults];
        
        
        NSDictionary* dic=[NSDictionary dictionary];
        [userdefault setObject:dic forKey:DATADIC];

        
        
       [userdefault setBool:NO forKey:ISLAND];

//      BOOL island=  [userdefault boolForKey:ISLAND];
        [userdefault synchronize];

        [self.navigationController popViewControllerAnimated:YES];
        
    }


}


@end
